package com.superman.supermarket.client.composite.orderlist.opt;

import java.util.HashMap;
import java.util.Map;

import com.superman.mvpframe.client.ajax.IPostMan;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ReloadEvent;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.widget.dialog.XConfirm;
import com.superman.mvpframe.client.widget.dialog.XConfirm.ConfirmCallBack;
import com.superman.supermarket.client.utils.ActivityJumpUrl;

/**
 * 订单操作按钮功能接口实例
 * 
 * @author superman
 * @version 2018年8月12日下午8:42:32
 */
public class OPButtonOperatorImpl implements OPButtonOperator {

	private IPostMan postman = XGinjector.INSTANCE.getPostMan();

	@Override
	public void afterSales(Map<String, Object> obj) {
	}

	@Override
	public void cancel(Map<String, Object> obj) {
		final String orderid = String.valueOf(obj.get("ORDER_ID"));

		XConfirm.confirm("您确定取消这个订单吗?", new ConfirmCallBack() {

			@Override
			public void cancel() {

			}

			@Override
			public void ok() {
				final Map<String, Object> map = new HashMap<String, Object>();
				map.put("ORDER_ID", orderid);
				postman.post("order/close", map, new PostManAsyncCallBack() {

					@Override
					public void onBusinessError(String statusCode) {

					}

					@Override
					public void onBusinessOK(String response) {
						XGinjector.INSTANCE.getEventbusController().fire(new ReloadEvent());
					}
				});
			}
		});

	}

	@Override
	public void closeRefundApplication(Map<String, Object> obj) {
		final String refundId = String.valueOf(obj.get("REFUND_ID"));
		XConfirm.confirm("您确定要撤销申请吗?", new ConfirmCallBack() {

			@Override
			public void cancel() {

			}

			@Override
			public void ok() {

				final Map<String, Object> map = new HashMap<String, Object>();
				map.put("REFUND_ID", refundId);
				postman.post("refund/close", map, new PostManAsyncCallBack() {

					@Override
					public void onBusinessError(String statusCode) {

					}

					@Override
					public void onBusinessOK(String response) {
						XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("已撤销"));
						XGinjector.INSTANCE.getEventbusController().fire(new ReloadEvent());
					}
				});

			}
		});
	}

	@Override
	public void comment(Map<String, Object> obj) {
		String orderid = String.valueOf(obj.get("ORDER_ID"));
		ActivityJumpUrl.pushToCommentPage(orderid);
	}

	@Override
	public void confirm(Map<String, Object> obj) {
		final String orderid = String.valueOf(obj.get("ORDER_ID"));

		XConfirm.confirm("您确定收到货了吗?", new ConfirmCallBack() {

			@Override
			public void cancel() {

			}

			@Override
			public void ok() {
				final Map<String, Object> map = new HashMap<String, Object>();
				map.put("ORDER_ID", orderid);
				postman.post("order/receipt", map, new PostManAsyncCallBack() {

					@Override
					public void onBusinessError(String statusCode) {

					}

					@Override
					public void onBusinessOK(String response) {
						XGinjector.INSTANCE.getEventbusController().fire(new ReloadEvent());
					}
				});
			}
		});

	}

	@Override
	public void deleteOrder(final Map<String, Object> obj) {
		final String orderid = String.valueOf(obj.get("ORDER_ID"));

		XConfirm.confirm("您确定删除这个订单吗?", new ConfirmCallBack() {

			@Override
			public void cancel() {

			}

			@Override
			public void ok() {
				final Map<String, Object> map = new HashMap<String, Object>();
				map.put("ORDER_ID", orderid);
				postman.post("order/delete", map, new PostManAsyncCallBack() {

					@Override
					public void onBusinessError(String statusCode) {

					}

					@Override
					public void onBusinessOK(String response) {
						XGinjector.INSTANCE.getEventbusController().fire(new ReloadEvent());
					}
				});
			}
		});

	}

	@Override
	public void logisticsState(Map<String, Object> obj) {
		// 判断是退款订单的物流还是普通订单的物流
		if (obj.containsKey("REFUND_ID")) {
			// 退货物流
			String refundid = String.valueOf(obj.get("REFUND_ID"));
			ActivityJumpUrl.pushToRefundLogisticsPage(refundid);
		} else {
			// 订单的查看物流
			String orderid = String.valueOf(obj.get("ORDER_ID"));
			ActivityJumpUrl.pushToOrderLogisticsPage(orderid);
		}
	}

	@Override
	public void pay(Map<String, Object> obj) {
		String orderId = String.valueOf(obj.get("ORDER_ID"));
		ActivityJumpUrl.pushToCasherDeskPage(orderId);
	}

	@Override
	public void refund(Map<String, Object> obj) {
		String orderId = String.valueOf(obj.get("ORDER_ID"));
		String skuID = String.valueOf(obj.get("SKU_ID"));
		ActivityJumpUrl.pushToRefundApplicationPage(orderId, skuID);
	}

	@Override
	public void refunding(Map<String, Object> obj) {
		String refundId = String.valueOf(obj.get("REFUND_ID"));
		ActivityJumpUrl.pushToRefundDetailPage(refundId);
	}

	@Override
	public void refundRefused(Map<String, Object> obj) {
		String refundId = String.valueOf(obj.get("REFUND_ID"));
		ActivityJumpUrl.pushToRefundDetailPage(refundId);

	}

	@Override
	public void refundSuccess(Map<String, Object> obj) {
		String refundId = String.valueOf(obj.get("REFUND_ID"));
		ActivityJumpUrl.pushToRefundDetailPage(refundId);
	}

	@Override
	public void returnGoods(Map<String, Object> obj) {
		String refundId = String.valueOf(obj.get("REFUND_ID"));
		ActivityJumpUrl.pushToRefundGoodsPage(refundId);
	}

	@Override
	public void ticketList(Map<String, Object> obj) {
		// Window.alert("todo:");
	}

	@Override
	public void updateRefundApplication(Map<String, Object> obj) {
		String refundId = String.valueOf(obj.get("REFUND_ID"));
		ActivityJumpUrl.pushToRefundModifyPage(refundId);
	}

}
